package com.wangan.wanganpush.services;

import com.wangan.wanganpush.dao.LoginDao;
import com.wangan.wanganpush.model.UserLogin;
import com.wangan.wanganpush.utils.FileNameParse;
import com.wangan.wanganpush.xml.UserLoginXml;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LoginImpl extends BaseImpl{

    @Autowired
    LoginDao loginDao;

    @Autowired
    GetXmlSendZipUtil getXmlSendZipUtil;

    @Autowired
    UserLoginXml userLoginXml;

    @Autowired
    FileNameParse fileNameParse;

    public Boolean userLoginPush() {
        try {
            //查询出需要推送的数据
            List<UserLogin> lognLi = loginDao.findAllByFlag(0);

            //调用推送方法
            if (lognLi != null && lognLi.size() > 0) {
                //拿取消息流水号
                String messageCode = fileNameParse.getMessageCode();

                String fileName =fileNameParse.parseFileName("LOGIN","3",messageCode);

                //把数据转xml字符串
                String str = userLoginXml.parseXml(lognLi,messageCode,fileName);

                //送往xml转zip包的流程并推送网安
                getXmlSendZipUtil.sendZip("LOGIN",str,messageCode,fileName,null);

            }
            System.out.println(lognLi.size());

            //调用父类方法更改标志位和时间
            lognLi = super.updateFlag(lognLi);

            //更改数据库flag字段为1已经发送
            if (lognLi != null && lognLi.size() > 0) {
                //更新数据
                loginDao.save(lognLi);
                //提交事务
                loginDao.flush();
            }

            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }


    }

}
